#include <REGX52.H>
#include "LCD1602.h"
#include "DS1302.h"
#include "Timer0.h"
#include "Key.h"

typedef unsigned char uint8;

uint8 isEditing = 0;
uint8 blinkToggle = 0;
uint8 editPos = 0;


unsigned char isLeapYear(unsigned int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

void main() {
	uint8 key;
	
	LCD_Init();
	Timer0_Init();
	DS1302_Init();
	DS1302_SetTime();
	
	LCD_ShowString(1, 1, "    -  -");
	LCD_ShowString(2, 1, "  :  :");

	while (1) {
		if (!isEditing)
			DS1302_GetTime();
		
		if (!isEditing || editPos != 0 || blinkToggle) {
			LCD_ShowNum(1, 1, 20, 2);
			LCD_ShowNum(1, 3, DS1302_Time[0], 2);
		} else {
			LCD_ShowString(1, 1, "    ");
		}
		
		if (!isEditing || editPos != 1 || blinkToggle)
			LCD_ShowNum(1, 6, DS1302_Time[1], 2);
		else 
			LCD_ShowString(1, 6, "  ");
		
		if (!isEditing || editPos != 2 || blinkToggle)
			LCD_ShowNum(1, 9, DS1302_Time[2], 2);
		else
			LCD_ShowString(1, 9, "  ");
		
		if (!isEditing || editPos != 3 || blinkToggle)
			LCD_ShowNum(2, 1, DS1302_Time[3], 2);
		else
			LCD_ShowString(2, 1, "  ");
		
		if (!isEditing || editPos != 4 || blinkToggle)
			LCD_ShowNum(2, 4, DS1302_Time[4], 2);
		else 
			LCD_ShowString(2, 4, "  ");
		
			
		if (!isEditing || editPos != 5 || blinkToggle)
			LCD_ShowNum(2, 7, DS1302_Time[5], 2);
		else
			LCD_ShowString(2, 7, "  ");
		
		key = Key();
		switch (key) {
			case 1: // 切换编辑模式
				DS1302_toggleEnableTick();
				isEditing = !isEditing;
				if (!isEditing)
					// 需要再对日期格式进行验证，主要验证年月日
					DS1302_SetTime();
				break;
			case 2: // 选择编辑的栏目
				editPos++;
				editPos %= 6;
				break;
			case 3: // -
				DS1302_Time[editPos]--;//时间设置位数值减1
				if(DS1302_Time[0]<0){DS1302_Time[0]=99;}//年越界判断
				if(DS1302_Time[1]<1){DS1302_Time[1]=12;}//月越界判断
				if( DS1302_Time[1]==1 || DS1302_Time[1]==3 || DS1302_Time[1]==5 || DS1302_Time[1]==7 || 
					DS1302_Time[1]==8 || DS1302_Time[1]==10 || DS1302_Time[1]==12)//日越界判断
				{
					if(DS1302_Time[2]<1){DS1302_Time[2]=31;}//大月
					if(DS1302_Time[2]>31){DS1302_Time[2]=1;}
				}
				else if(DS1302_Time[1]==4 || DS1302_Time[1]==6 || DS1302_Time[1]==9 || DS1302_Time[1]==11)
				{
					if(DS1302_Time[2]<1){DS1302_Time[2]=30;}//小月
					if(DS1302_Time[2]>30){DS1302_Time[2]=1;}
				}
				else if(DS1302_Time[1]==2)
				{
					if(isLeapYear(2000 + DS1302_Time[0]))
					{
						if(DS1302_Time[2]<1){DS1302_Time[2]=29;}//闰年2月
						if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
					}
					else
					{
						if(DS1302_Time[2]<1){DS1302_Time[2]=28;}//平年2月
						if(DS1302_Time[2]>28){DS1302_Time[2]=1;}
					}
				}
				if(DS1302_Time[3]<0){DS1302_Time[3]=23;}//时越界判断
				if(DS1302_Time[4]<0){DS1302_Time[4]=59;}//分越界判断
				if(DS1302_Time[5]<0){DS1302_Time[5]=59;}//秒越界判断
				break;
			case 4: // +
				DS1302_Time[editPos]++;//时间设置位数值加1
				if(DS1302_Time[0]>99){DS1302_Time[0]=0;}//年越界判断
				if(DS1302_Time[1]>12){DS1302_Time[1]=1;}//月越界判断
				if( DS1302_Time[1]==1 || DS1302_Time[1]==3 || DS1302_Time[1]==5 || DS1302_Time[1]==7 || 
					DS1302_Time[1]==8 || DS1302_Time[1]==10 || DS1302_Time[1]==12)//日越界判断
				{
					if(DS1302_Time[2]>31){DS1302_Time[2]=1;}//大月
				}
				else if(DS1302_Time[1]==4 || DS1302_Time[1]==6 || DS1302_Time[1]==9 || DS1302_Time[1]==11)
				{
					if(DS1302_Time[2]>30){DS1302_Time[2]=1;}//小月
				}
				else if(DS1302_Time[1]==2)
				{
					if(isLeapYear(2000 + DS1302_Time[0]))
					{
						if(DS1302_Time[2]>29){DS1302_Time[2]=1;}//闰年2月
					}
					else
					{
						if(DS1302_Time[2]>28){DS1302_Time[2]=1;}//平年2月
					}
				}
				if(DS1302_Time[3]>23){DS1302_Time[3]=0;}//时越界判断
				if(DS1302_Time[4]>59){DS1302_Time[4]=0;}//分越界判断
				if(DS1302_Time[5]>59){DS1302_Time[5]=0;}//秒越界判断
				break;
		}
	}

}

void Timer0_Isr(void) interrupt 1
{
	static unsigned int timer0Counter = 0;
	timer0Counter++;
	Timer0_Reload();
	
	if (timer0Counter >= 500) {
		timer0Counter = 0;
		if (isEditing) {
			blinkToggle = !blinkToggle;
		}
	}
}